<?php
// +---------------------------------------------------------------------------+
// | This file is part of the KnowledgebasePublisher package                   |
// | KnowledgebasePublisher - web based knowledgebase publisher tool           |
// |                                                                           |
// | Author:  Evgeny Leontev <eleontev@gmail.com>                              |
// | Copyright (c) 2005 Evgeny Leontev                                         |
// |                                                                           |
// | For the full copyright and license information, please view the LICENSE   |
// | file that was distributed with this source code.                          |
// +---------------------------------------------------------------------------+

class SettingView_form extends AppView
{
	
	var $tmpl = 'form.html';
	
	
	
	function execute(&$obj, &$manager) {
		
		$tpl = &new tplTemplatez($this->template_dir . $this->tmpl);
		$tpl->tplAssign('error_msg', AppMsg::errorBox($obj->errors, $manager->module_name));
		
		
		// in demo mode 
		$msg_key = $this->controller->getRequestVar('show_msg');
		if($msg_key) {
			$tpl->tplAssign('error_msg', AppMsg::afterActionBox($msg_key, 'error'));
		}	
		
		
		$form_data = $this->parseMultiIni($this->template_dir . 'form.ini');
		
		$file = AppMsg::getModuleMsgFile($manager->module_name, 'setting_msg.ini');
		$setting_msg = AppMsg::parseMsgs($file, false, 1, true);

		$r = new Replacer();
		
		$select = new FormSelect();
		$select->select_tag = false;
		
		$parser = &$manager->getParser();
		$rows = &$manager->getRecords();

		$i = 1;
		foreach($rows as $group_id => $group) {
			
			if(!empty($setting_msg['group_title'])) {
				$tpl->tplSetNeeded('row/group');
				if($i != 1) { $tpl->tplSetNeeded('row/group_delim'); }
				$i++;
			}
				
			foreach($group as $setting_key => $v) {
				
				$setting_key = trim($setting_key);
				$v['group_title_msg'] = $setting_msg['group_title'][$v['group_id']];
				$v['required_sign'] = ($v['required']) ? '<span class="requiredSign">*</span>' : '';
				$v['id'] = $setting_key;
				
				
				if($v['input'] == 'checkbox' && $obj->get($setting_key) == 1) {
					$v['checked'] = 'checked';
				
				} elseif ($v['input'] == 'select') {
					
					// we have options in lang file
					if(isset($setting_msg[$v['setting_key']]['option_1'])) {
						
						$options = $parser->parseSelectOptions($v['setting_key'], $setting_msg[$v['setting_key']]);
						unset($options['title'], $options['descr']);
						foreach($v['range'] as $k1 => $v1) {
							$k1 = trim($k1);
							$v['range'][$k1] = trim(current($options));
							next($options);
						}
					}
					
					//if multiple
					if(strpos($v['options'], 'multiple') !== false) {
						$v['array_sign'] = '[]';
					}
					
					$select->setRange($v['range']);
					$v['value'] = $select->select($obj->get($setting_key));
					unset($v['range']);
				
				} else {
					
					$v['value'] = $obj->get($setting_key);
				}			 
				
				// here we can change some values
				$v['value'] = $parser->parseOut($setting_key, $v['value']);
				
				
				if($parser->parseForm($setting_key, 'check')) {
					$field = $parser->parseForm($setting_key, 
					                           $v, 
											   $r->parse($form_data[$v['input']], $v), 
											   $setting_msg);
				} else {				
					$field = $r->parse($form_data[$v['input']], $v);
				}
				
				
				$tpl->tplAssign('title_msg', $setting_msg[$v['setting_key']]['title']);
				$tpl->tplAssign('description_msg', $setting_msg[$v['setting_key']]['descr']);			
				$tpl->tplAssign('form_input', $field);
				
				//echo "<pre>"; print_r($v); echo "</pre>";
				$tpl->tplParse($v, 'row');
			}
		}
			
		
		//$tpl->tplAssign($this->setCommonFormVars($obj));
		//$tpl->tplAssign($obj->get());
		$tpl->tplAssign($this->msg);
		
		$tpl->tplParse();
		return $tpl->tplPrint(1);
	}
	
	
	// reassigned from AppView
	//function setModuleMsg() {
	//	$file = AppMsg::getModuleMsgFile($manager->module_name, 'setting_msg.ini');
	//	$this->msg_setting = GetMsgHelper::parseIni($file);
	//}
	
	
	// parse multilines ini file
	// it will skip all before defining first [block] 
	function parseMultiIni($file, $key = false) {
		$s_delim = '[%';
		$e_delim = '%]'; 
		
		$str = implode('',file($file));
		if($key && strpos($str, $s_delim . $key . $e_delim) === false) { return; } 
		
		$str = explode($s_delim, $str);
		$num = count($str);
			
		for($i=1;$i<$num;$i++){
			$section = substr($str[$i], 0, strpos($str[$i], $e_delim));
			$arr[$section] = substr($str[$i], strpos($str[$i], $e_delim)+strlen($e_delim));
		}
		
		return ($key) ? @$arr[$key] : $arr;
	}
}
?>